package com.gin.kotlin.no_gay_alliance.database.converter

import androidx.room.TypeConverter
import com.gin.nga.enums.NoticeType
import com.gin.nga.enums.status.TopicStatus
import java.time.Instant
import java.time.ZoneId
import java.time.ZonedDateTime

/**
 * 类型转换器
 */
class MyTypeConverter {
    companion object {
        @TypeConverter
        @JvmStatic
        fun intToTopicStatus(ordinal: Int?) = TopicStatus.entries.firstOrNull { it.ordinal == ordinal }

        @TypeConverter
        @JvmStatic
        fun intToNoticeType(id: Int?) = NoticeType.entries.firstOrNull { it.id == id }

        @TypeConverter
        @JvmStatic
        fun noticeTypeToInt(noticeType: NoticeType?) = noticeType?.id

        @TypeConverter
        @JvmStatic
        fun titleColorToInt(titleColor: TopicStatus?) = titleColor?.ordinal

        @TypeConverter
        @JvmStatic
        fun zdtToLong(zdt: ZonedDateTime?): Long? = zdt?.toEpochSecond()

        @TypeConverter
        @JvmStatic
        fun longToZdt(time: Long?): ZonedDateTime? = time?.let { ZonedDateTime.ofInstant(Instant.ofEpochSecond(it), ZoneId.systemDefault()) }
    }
}